/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controller;

import Model.JConexion;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Gaby
 */
public class C_Principal extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    String query = "";
    String nombre = "";
    String apellido_p = "";
    String apellido_m = "";
    String direccion = "";
    String ciudad = "";
    String telefono = "";
    String tc = "";
    String username = "";
    String password = "";
    JConexion jc = new JConexion("");
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, InstantiationException, IllegalAccessException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            this.nombre = request.getParameter("nombre");
            this.apellido_p = request.getParameter("apellido_p");
            this.apellido_m = request.getParameter("direccion");
            this.direccion = request.getParameter("direccion");
            this.ciudad = request.getParameter("ciudad");
            this.telefono = request.getParameter("telefono");
            this.tc = request.getParameter("tc");
            this.username = request.getParameter("username");
            this.password = request.getParameter("password");
            
            System.out.println("nombre: " + this.nombre + " " + this.apellido_p + " " + this.apellido_m);
            System.out.println("direccion: " + this.direccion + ", " + this.ciudad );
            System.out.println("telefono: " + this.telefono);
            System.out.println("tc: " + this.tc);

            this.query = "Insert Into royal_mowes.cat_usuario (Nombre, Apellido_P, Apellido_M, Direccion, Ciudad, Telefono, TC, Username, Pass) Values (\"" + this.nombre + "\", \"" + this.apellido_p + "\", \"" + this.apellido_m + "\", \"" + this.direccion + "\", \"" + this.ciudad + "\", \"" + this.telefono + "\", \"" + this.tc + "\", \"" + this.username + "\", \"" + this.password + "\")";
            System.out.println(this.query);

            jc.MySQLArray("MySQLRM", "Insert", this.query);

            out.println("<html><head><title>Redireccionado</title></head>"
                        + "<body><center><br><br><big><big><font face=\"verdana\">Usuario Registrado</font></big></big><br><br><br><br>"
                        + "<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/alta_usuarios.jsp\">Regresar</a>"
                        + "&nbsp;&nbsp;&nbsp;&nbsp<a href=\"http://"+ request.getLocalName() + ":" + request.getLocalPort() +"/RoyalMowes/login.jsp\">Paginal Principal</a></center></body></html>");

            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet C_Principal</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet C_Principal at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_Principal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (InstantiationException ex) {
            Logger.getLogger(C_Principal.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(C_Principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
